<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加电影信息</title>
    <script src="../js/jquery.js"></script>
</head>
<style>
    table{
        background-color: aquamarine;
    }
    table td{
        border: 1px solid #333;
    }
</style>
<body>
<div>
    <h1>添加电影信息</h1>
    <table>
        <tr>
            <td>电影名称:</td>
            <td><input id="movieName" type="text">
            <span id="movieNameHint" style="color: red">电影名称不能为空！</span>
            </td>
        </tr>
        <tr>
            <td>电影类型:</td>
            <td><input id="movieType" type="text">
                <span id="movieTypeHint" style="color: red">电影类型不能为空</span>
            </td>
        </tr>
        <tr>
            <td>电影价格:</td>
            <td><input id="price" type="text"></td>
        </tr>
        <tr>
            <td>是否推荐:</td>
            <td>
                <input class="recommend" type="radio" checked="checked" value="1"/>推荐
                <input type="radio" class="recommend" value="0">不推荐
            </td>
        </tr>
        <tr>
            <td>上映时间:</td>
            <td><input id="startTime" type="text">&nbsp;格式: yyyy-MM-dd</td>
        </tr>
        <tr>
            <td>下映时间:</td>
            <td><input id="endTime" type="text">&nbsp;格式: yyyy-MM-dd</td>
        </tr>
        <tr>
            <td>投票数</td>
            <td><input id="votes" type="text"></td>
        </tr>
        <tr>
            <td>操作:</td>
            <td>
                <input id="add" type="button" value="添加">
                <input id="back" type="button" value="返回">
            </td>
        </tr>
    </table>
</div>
</body>
</html>
<script>
    $("#movieNameHint").hide();
    $("#movieTypeHint").hide();
    $(".recommend").on("click", function() {
        let value = $(this).val()
        $(".recommend").prop("checked", false)
        $(this).prop("checked", true)
        console.log("选中的值： " + value)
    })

    $('#back').click(function (){
        window.location.href = "home.jsp"
    })
    

    $('#add').click(function (){
        let movieName = $('#movieName').val()
        let opr = "selectMovieByMovieName"
        $.ajax({
            'url': '../MovieServlet.action',
            'type': 'post',
            'data': {opr,movieName},
            'dataType': 'json',
            'success': selectMovieByMovieNameSuccess
        })
    })

    function selectMovieByMovieNameSuccess(data) {
        if (data == "ok"){
            alert("电影名称重复!")
        }else {
            let movieName = $('#movieName').val()
            let movieType = $('#movieType').val()
            let price = $('#price').val()
            let recommend = $('.recommend').val()
            let startTime = $('#startTime').val()
            let endTime = $('#endTime').val()
            let votes = $('#votes').val()
            if (verify(movieName,movieType,price,startTime,endTime,votes)){
                let opr = "addMovie"
                $.ajax({
                    'url': '../MovieServlet.action',
                    'type': 'post',
                    'data': {opr,movieName,recommend,movieType,price,startTime,endTime,votes},
                    'dataType': 'json',
                    'success': addMovieSuccess
                })
            }
        }
    }

    function addMovieSuccess(data) {
        if (data == 'ok'){
            alert("添加成功!")
            window.location.href = "home.jsp"
        }else {
            alert("添加失败!")
        }
    }

    
    
    function verify(movieName,movieType,price,startTime,endTime,votes) {
        if (!movieName){
            $('#movieNameHint').show()
            return false
        }
        $('#movieNameHint').hide()
        if (!movieType){
            $('#movieTypeHint').show()
            return false
        }
        $('#movieTypeHint').hide()
        if (isNaN(price) || price <= 0) {
            $('#priceHint').show();
            alert("请输入正确的价格")
            return false;
        }
        if (isNaN(votes) || votes <= 0) {
            $('#votesHint').show();
            alert("请输入正确的票数")
            return false;
        }

        let dateRegex = /^\d{4}-\d{2}-\d{2}$/;
        if (!dateRegex.test(startTime) && !dateRegex.test(endTime)) {
            alert("请输入正确的日期格式：yyyy-MM-dd");
            return false;
        }

        return true;
    }


</script>